<template>
    <div class="meshWarp" v-if="dataObj !== null">

        <div class="meshContainer">

            <div class="categoryItem">
                <span class="categoryItemTitle">Mesh名称</span>
                <span class="meshName">
                        <input @keydown="renameMeshInput"
                               type="text"
                               v-model="dataObj.name"
                               disabled="disabled"
                        >&nbsp;

                        <i class="icon iconfont icon-xiugai editor-ico"  @click="renameMesh"></i>
                    </span>
            </div>

            <div class="categoryItem">
                <span class="categoryItemTitle">坐标</span>

                <span class="position">
                <div>
                    <span class="shadowBox">&nbsp; X
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.position.x.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['position','x'])">
                    </span>
                     <span class="shadowBox">&nbsp; Y
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.position.y.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['position','y'])">
                    </span>
                    <span class="shadowBox">&nbsp; Z
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.position.z.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['position','z'])">
                    </span>
                 </div>
             </span>
            </div>

            <div class="categoryItem">
                <span class="categoryItemTitle">旋转</span>

                <span class="position">
                <div>
                    <span class="shadowBox">&nbsp; X
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.rotation._x.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['rotation','x'])">
                    </span>
                     <span class="shadowBox">&nbsp; Y
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.rotation.y.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['rotation','y'])">
                    </span>
                    <span class="shadowBox">&nbsp; Z
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.rotation.z.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['rotation','z'])">
                    </span>
                 </div>
             </span>
            </div>

            <div class="categoryItemBottom">
                <span class="categoryItemTitle">缩放</span>

                <span class="position">
                <div>
                    <span class="shadowBox">&nbsp; X
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.scale.x.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['scale','x'])">
                    </span>
                     <span class="shadowBox">&nbsp; Y
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.scale.y.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['scale','y'])">
                    </span>
                    <span class="shadowBox">&nbsp; Z
                        <input class="positionInput" type="number" step="0.01"
                               v-bind:value="dataObj.scale.z.toFixed(2)"
                               v-on:keydown="changeVal($event, dataObj, ['scale','z'])">
                    </span>
                 </div>
             </span>
            </div>

            <div class="categoryItem">
                <span class="categoryItemTitle showTitle">显示</span>
                <span class="radioBtn titleRadioBtn" @click="toggleClass($event, dataObj.visible, 'visible')" ref="visibleRadioBtn"></span>
            </div>

            <div class="shadowContainer meshShadow">
                <span class="shadowTitle">阴影</span>

                <span class="radioBtn" @click="toggleClass($event, dataObj.castShadow, 'castShadow')" ref="castShadowRadioBtn"></span>&nbsp;

                <span>投射</span>

                <span class="radioBtn" @click="toggleClass($event, dataObj.receiveShadow, 'receiveShadow')" ref="receiveShadowRadioBtn"></span>&nbsp;

                <span>接收</span>
            </div>

            <div class="categoryItem" v-if="dataObj.material">
                <span class="categoryItemTitle materialTitle">材质</span>
                <span class="materialItem" v-for="(v, k) in materialSetData" :key="k">
                    <span>{{v.name}}</span>
                </span>
            </div>
        </div>


    </div>
</template>

<script>

    import $ from "jquery";
    import DomUtil from "@/utils/baseUtil.js";
    import Vue from "vue";

    export default {
        name: "mesh",

        data () {
            return {
                dataObj: this.$props.data,

                materialSetData: [],

                materialIndex: -1,
            }
        },

        watch : {
            intensityId: {

                handler () {

                    this.dataObj = this.$props.data || null;

                    this.setRadioBtnType();

                    this.setMaterialSetData();

                    this.$nextTick(() => {

                        const mesh = $(".editorPickContainer .mesh");
                        const meshItemContainer = $(".editorPickContainer .mesh .meshItemContainer");

                        if (mesh.css("display") === "none") {
                            mesh.show();
                            meshItemContainer.show();
                        }

                    });
                }
            },
        },

        props: ["data", "intensityId"],

        created () {
            this.setMaterialSetData();
        },

        mounted () {

            const _this = this;

            this.setRadioBtnType();

            this.$nextTick(() => {

                const mesh = $(".editorPickContainer .mesh");
                const meshItemContainer = $(".editorPickContainer .mesh .meshItemContainer");

                if (mesh.css("display") === "none") {
                    mesh.show();
                    meshItemContainer.show();
                }

            });

            this.$root.$on("MeshValueChanged", _this.reloadData);
        },

        methods: {

            toggleClass(e, val, attr) {

                $(e.target).toggleClass("el-icon-check");

                val = !val;

                this.dataObj[attr] = val;
            },

            slideToggleAni(e) {
                let target = e.target;

                if (!$(target).hasClass("title") && !$(target).hasClass("el-switch__core") &&
                    !$(target).hasClass("el-icon-delete deleteIco")) {
                    target = $(target).parent(".title");
                }

                if (!$(target).hasClass("el-icon-delete deleteIco")) {
                    $(target).toggleClass('titleActive');
                    $(target).siblings().eq(0).slideToggle(200);
                }
            },

            changeVal (e, data, paramList) {

                if (e.keyCode === 13 && paramList.length > 0) {

                    const target = e.target;

                    let value = {};

                    paramList.forEach(attr => value = value[attr] || data[attr] || 0);

                    this.$store.dispatch('edited', {
                        target: data,
                        value: value,
                        paramList: paramList
                    });

                    DomUtil.changeVal(target, data, paramList);

                }

            },

            switchOnChanged() {
                this.switchFlag = !this.switchFlag;
            },

            deleteClicked() {

                if (this.dataObj.parent !== null) {

                    this.dataObj.parent.remove(this.dataObj);

                    /* eslint-disable */
                    Vue.prototype.baseConfig.light = Editor.getLights();

                }

                this.$root.$emit("outLineDataChanged");
                this.$root.$emit("editorDataChanged");
            },

            reloadData() {
                this.$forceUpdate();
            },

            setRadioBtnType() {

                $(this.$refs.visibleRadioBtn).removeClass("el-icon-check");
                $(this.$refs.castShadowRadioBtn).removeClass("el-icon-check");
                $(this.$refs.receiveShadowRadioBtn).removeClass("el-icon-check");

                if (this.dataObj.visible) {
                    $(this.$refs.visibleRadioBtn).addClass("el-icon-check");
                }

                if (this.dataObj.castShadow) {
                    $(this.$refs.castShadowRadioBtn).addClass("el-icon-check");
                }

                if (this.dataObj.receiveShadow) {
                    $(this.$refs.receiveShadowRadioBtn).addClass("el-icon-check");
                }

            },

            renameMeshInput (e) {

                if (e.keyCode === 13) {

                    $(e.target).attr("disabled", "disabled");

                    const name = $.trim($(e.target).val());

                    if (name) {
                        let nameStr = "";

                        if (this.dataObj.uuid) {
                            nameStr = this.dataObj.name;
                        } else {
                            nameStr = this.dataObj.material.name;
                        }

                        const scene = Halo.Config.scene();

                        // eslint-disable-next-line
                        Editor.rename("mat", nameStr, name);

                        Editor.save("scn", scene.name, scene);
                    }

                }
            },

            renameMesh (e) {
                $(e.target).siblings().removeAttr("disabled");
                $(e.target).siblings().focus();
            },

            isMesh: (childArray) => childArray.filter(item => item.type === "Mesh"),

            setMaterialSetData () {

                const material = this.dataObj.material;
                const _this = this;

                if (material instanceof Array) {
                    material.forEach(val => _this.materialSetData.push(val));
                } else {
                    _this.materialSetData[0] = material;
                }
            },
        }
    }
</script>

<style scoped>

    .meshWarp {
        width: 100%;
        color: rgba(182, 184, 220, 1);
        box-sizing: border-box;
        margin: 0 auto;
        border-radius: 5px;
        position: relative;
    }

    .meshWarp .meshContainer {
        position: relative;
    }

    .title {
        width: 100%;
        height: 30px;
        background:rgba(65,67,91,1);
        font-size: 12px;
        border-radius: 5px;
        position: relative;
        line-height: 30px;
        cursor: pointer;
    }

    .titleActive {
        background-color: rgba(105, 108, 145, 1);
        border-radius: 5px 5px 0 0;
    }

    .titleActive {
        transition: .5s all;
        transform: rotateZ(90deg);
    }

    .meshContainer {
        background-color: rgba(52,54,74,1);
        border-radius: 0 0 5px 5px;
        border-top: 1px solid rgba(31,33,49,1);
        padding-bottom: 8px;
    }

    .title span {
        position: absolute;
        left: 34px;
        max-width: 120px;
        overflow: hidden;
    }

    .title:hover span, .title:hover {
        color: rgba(213, 215, 250, 1);
    }

    .el-icon-delete.deleteIco {
        position: absolute;
        right: 70px;
        top: 9px;
    }

    .position {
        display: inline-block;
        width: 220px;
        height: 30px;
        background-color: rgba(31, 33, 49, 1);
        border-radius:5px;
        line-height: 30px;
        margin-left: 44px;
    }

    .position div {
        display: flex;
        justify-content: space-around;
    }

    .positionInput {
        border: none;
        background-color: transparent;
        width: 39px;
        color: rgba(182,184,220,1);
        font-size: 13px;
        vertical-align: baseline;
        box-sizing: border-box;
    }

    .shadowContainer {
        display: inline-block;
        width: 276px;
        height: 30px;
        background: rgba(65, 67, 91, 1);
        border-radius: 5px;
        line-height: 30px;
        margin: 14px 0 0 34px;
        font-size: 12px;
    }

    .shadowTitle {
        padding: 0 13px 0 14px;
    }

    .shadowBox {
        font-size: 13px;
        font-weight: 300;
        color: rgba(65,67,91,1);
    }
    .categoryItem {
        padding-top: 14px;
    }

    .categoryItemBottom {
        padding: 14px 0 20px 0;
    }

    .categoryItemTitle {
        font-size: 12px;
        font-weight: 100;
        color: rgba(182,184,220,1);
        padding-left: 32px;
    }

    .materialTitle {
        padding-left: 32px !important;
    }

    .radioBtn {
        display: inline-block;
        width: 14px;
        height: 12px;
        border: 2px solid rgba(47, 48, 66, 1);
        border-radius: 2px;
        position: relative;
        cursor: pointer;
        margin-left: 17px;
        box-sizing: border-box;
        vertical-align: middle;
        padding: 0;
    }

    .titleRadioBtn {
        margin-left: 60px;
    }

    .radioBtn.el-icon-check {
        font-weight: bold;
        font-size: 12px;
        color: rgba(113, 215, 203, 1);
    }

    .showTitle {
        font-size: 14px;
        margin: 20px 0 0 14px;
        padding: 0;
    }

    .meshShadow {
        margin-left: 14px;
    }


    .meshWarp .categoryItem .meshName input {
        position: absolute;
        width: 180px;
        height: 30px;
        text-align: right;
        right: 16px;
        background: transparent;
        border: none;
        font-size:14px;
        font-weight:100;
        color:rgba(182,184,220,1);
    }

    .meshWarp .categoryItem .meshName {
        display: inline-block;
        width: 180px;
        height: 30px;
        line-height: 30px;
        margin-left: 44px;
        position: relative;
    }

    .meshWarp .categoryItem .meshName i {
        position: absolute;
        top: 0 !important;
        right: 0;
        cursor: pointer;
        font-size:14px;
        color: rgba(105, 108, 145, 1);
    }

    .meshWarp .categoryItem .meshName i:hover {
        color: rgba(180, 183, 218, 1);
    }

    .editedmeshlBtn {
        position: absolute;
        right: 0;
        top: 8px;
        width:62px;
        height:30px;
        background:rgba(65,67,91,1);
        border-radius:5px;
        font-size:14px;
        font-weight:300;
        color:rgba(182,184,220,1);
    }

    .editedmeshlBtn:hover {
        background:rgba(105,108,145,1);
    }

    .meshWarp .materialItem {
        display: inline-block;
        width: 220px;
        height: 30px;
        line-height: 30px;
        margin-left: 44px;
        position: relative;
    }

    .meshWarp .materialItem span {
        position: absolute;
        width: 180px;
        height: 30px;
        overflow: hidden;
        right: 70px;
        top: 8px;
        font-size:14px;
        font-weight:100;
        color:rgba(182,184,220,1);
        background:rgba(31,33,49,1);
        border-radius:5px;
        text-align: center;
    }
</style>